<?php
	require_once("../process/session.php");
	$my_session = new session();
	$db = $my_session->getDB_torneig();
?>
<html>
	<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Generador d'horaris</title>
        <link rel="stylesheet" type="text/css" href="../estils/style.css">
	</head>
	<body>
    	<div id="capçal" class="ombra">
    	
    		<h1 id="h1_head"> Generador d'horaris<h1>

		</div>
		
		<div id="main" class="ombra">
			<div id="menu">
				<ul>
					<li class="nivell1"><a class="nivell1" href="../forms/index.php">Inici</a></li>
					<li class="nivell1"><a class="nivell1" href="#">Participants</a>
						<ul class="pestanya"><span></span>
							<li><a href="../forms/f_inscrip.php">Inscriure's</a></li>
							<li><a href="#">Informació</a></li>
							<li><a href="#">Fotos</a></li>
						</ul>
					</li>
					<li class="nivell1"><a class="nivell1" href="#">Organització</a>
						<ul class="pestanya"><span></span>
							<li><a href="../forms/f_admin.php">Administrar torneig</a></li>
							<li><a href="#">Crear torneig</a></li>
						</ul>
					</li>
				</ul>
			</div>	

			 
			<h2> Configurar <?php echo $_SESSION['torneig'];?> </h2> <br>
				<div class="requadre">
				<form name="afegir_cat" action="../process/p_admin_torneig.php" method="POST" accept-charset="utf-8">
						<h3> Afegir Categoria </h3>
						 
						Nom categoria: <input type="text" name="nom_cat"> <br>
						Descripció: <TEXTAREA COLS=30 ROWS=8 NAME="desc_cat"> </TEXTAREA> <br>
						
						<h3> Sexe </h3>
						<input type="radio" name="sexe" value="masculi"> Masculí  <br>
						<input type="radio" name="sexe" value="femeni"> Femení <br>
						<input type="radio" name="sexe" value="mixte"> Mixte <br>
						
						<h3> Nivell </h3>
						<input type="radio" name="nivell" value="infantil"> Infantil <br>				
						<input type="radio" name="nivell" value="costellada"> Costellada <br>
						<input type="radio" name="nivell" value="obert"> Obert<br>
						<input type="radio" name="nivell" value="elit"> Èlit <br>
						
						Mínim jugadors: <input type="text" name="min_part"> <br>
						Màxim jugadors: <input type="text" name="max_part"> <br>
						<input type="hidden" name="function" value="afegir_cat">
						<input type="submit" value="Afegir">
					
				</form>
			</div>
			<div class="requadre">
				<h3> Eliminar Categoria </h3>
				<?php					
					// TODO Verificar que $res té dades correctes
					$query = "Select nom From categories";
					$res = $db->consulta($query);
				?>
				<h3> Categories </h3>
				<form action="../process/p_admin_torneig.php" name="categories" method="post">
				    <select name="eliminar_cat">
				<?php
				 	for ($i=0; $i < sizeof($res); $i++) { 
				?>
				    <option value="<?php echo $res[$i]['nom']; ?>"><?php echo $res[$i]['nom']; ?></option>
				<?php
				    }
				?>
				    </select>
				<?php
				    //mysql_close($db);
				?>
				<input type="hidden" name="function" value="eliminar_cat">
				<input type="submit" value="Eliminar">
				</form>
				
				
			</div>
			
				<div class="requadre">
				<form name="afegir_pista" action="../process/p_admin_torneig.php" method="POST" accept-charset="utf-8">
						<h3> Afegir Pista </h3>
						 
						Nom Pista: <input type="text" name="nom_pista"> <br>
						
						<h3> Categories permeses </h3>
						<?php
							$query = "Select nom From categories";
							$res = $db->consulta($query);
							
							for($i=0; $i < sizeof($res); $i++){
						?>
						<input type="checkbox" name="cat_pista<?php echo $i?>" value="<?php echo $res[$i]['nom']; ?>"> <?php echo $res[$i]['nom']; ?>  <br>
						<?php
							}
						?>
						<input type="hidden" name="function" value="afegir_pista">
						<input type="submit" value="Afegir">
					
				</form>
			</div>
			<div class="requadre">
				<h3> Eliminar Pista </h3>
				<?php
					// TODO Verificar que $res té dades correctes
					$query = "Select nom From pistes";
					$res = $db->consulta($query);
				?>
				<h3> Pistes </h3>
				<form action="../process/p_admin_torneig.php" name="pistes" method="post">
				    <select name="eliminar_pista">
				<?php
				 	for ($i=0; $i < sizeof($res); $i++) { 
				?>
				    <option value="<?php echo $res[$i]['nom']; ?>"><?php echo $res[$i]['nom']; ?></option>
				<?php
				    }
				?>
				    </select>
				<?php
				    //mysql_close($db);
				?>
				<input type="hidden" name="function" value="eliminar_pista">
				<input type="submit" value="Eliminar">
				</form>
			</div>
			
			<?php
				$query = "Select * From hores_dies Order By dia";
				$hores = $db->consulta($query);
			?>
			
			<script>
				var n_dies_vell = <?php echo sizeof($hores) ?>;
				var hores_ini = new Array(n_dies_vell);
				var hores_fi = new Array(n_dies_vell);
				<?php 
					for($i = 0; $i < sizeof($hores); $i++){			
				?>
					hores_ini[<?php echo $i?>] = <?php echo $hores[$i]['h_ini']?>;
					hores_fi[<?php echo $i?>] = <?php echo $hores[$i]['h_fi']?>;
				<?php 
					}
				?>
			
				function afegirRequadres(){
					//document.getElementById('hores').innerHTML = 'Dia 1 <input type="text" name="hora1"> <br>';  

					var n_dies = document.dies.n_dies.selectedIndex+1;
					var txt = '';
					
					for (var i=1; i <= n_dies; i++) {
					 txt += 'Dia '+ i + ': Inici ';
					 txt += '<select name="sel_ini_'+i+'">';
					  
						  for (var j=8; j <= 24; j++) {
						  	if(i <= n_dies_vell && j == hores_ini[i-1]){
						  		txt += '<option value="'+j+'" selected="selected">'+j+'</option>';
						  	} else {
						  		txt += '<option value="'+j+'">'+j+'</option>';
						  	};
						  };
						  txt += '</select>';
						  txt += ' Fi ';
						  txt += '<select name="sel_fi_'+i+'">';
						  for (var j=8; j <= 24; j++) {
							if(i <= n_dies_vell && j == hores_fi[i-1]){
						  		txt += '<option value="'+j+'" selected="selected">'+j+'</option>';
						  	} else {
						  		txt += '<option value="'+j+'">'+j+'</option>';
						  	};
						  };
						  txt += '</select>';
						  txt += '<br>';
					};
					txt += '<input type="hidden" name="function" value="hores">'
					txt += '<input type="submit" value="Guardar">';
					document.getElementById('hores').innerHTML = txt;
				}
			</script>
			

			
			<div class="requadre">
				<h3> Durada torneig (dies)</h3>
				<form action="../process/p_admin_torneig.php" name="dies" method="post">
					<select name="n_dies" onclick="afegirRequadres()">
						<?php
							$sel = -1;
							if(sizeof($hores) > 0 && sizeof($hores) <= 10)	$sel = sizeof($hores);
							for ($i=0; $i < 10; $i++) { 
						?>
							<option value="<?php echo $i+1; ?>"<?php if($i == $sel-1)	echo ' selected="selected"';?> ><?php echo $i+1; ?></option>
							
						<?php
							}
							
							
						?>

					</select>
					<div id="hores"> 
					
					</div>
				</form>
			</div>
			<div class="requadre">
				<h3> Altres </h3>
				<form action="../process/p_admin_torneig.php" name="altres" method="post">
						<h3> Restriccions </h3>
						<input type="radio" name="restr" value="no" CHECKED> No permèses <br>
						<input type="radio" name="restr" value="una"> Una en total <br>
						<input type="radio" name="restr" value="una_x_2dies"> Una cada dos dies <br>
						<h3> Pagament </h3>
						<input type="radio" name="pago" value="no" CHECKED> No <br>
						<input type="radio" name="pago" value="si" > Si <br>
						<input type="hidden" name="function" value="altres">
						<input type="submit" value="Enviar">
				</form>
			</div>
		</div>
	</body>
</html>